Ext.onReady(function(){
		Ext.BLANK_IMAGE_URL = "extjs/resources/images/default/s.gif";
	    Ext.QuickTips.init();
		mainWindow.show();
		
});


var fielmenu=new Ext.menu.Menu({
		items:[{text:'期初账款设定',handler:function(){
			fromaction();
		}},{text:'原产物料设定'}]
	});
	var fielmenu2=new Ext.menu.Menu({
		items:[{text:'上网'},{text:'写信'},{text:'历史交易查询'},{text:'帐额数据效验'}]
	});


var grids=Ext.create('Ext.grid.Panel',{
	 listeners:{
						containerdblclick: function(grid, e, eOpts){ //单击事件
	    						grid.getStore().add({'name':'', 'dizhi':'','bianma':'','lianxi':''});
    						
    					}
				
			},plugins: [
        				Ext.create('Ext.grid.plugin.CellEditing', {
            				clicksToEdit: 1
        				})
    				],
	columns: [
				 {header: '（栏号）',
	                    		xtype: 'rownumberer',
	                    		dataIndex: 'lh', 
	                    		width: 50,
								sortable: false
        					},
					
        		{ header: '地址编号',  dataIndex: 'name',editor: { xtype:'textfield'}},
       			{ header: '地址', dataIndex: 'dizhi', flex: 1,editor: { xtype:'textfield'}},
       				  { header: '邮政编码', dataIndex: 'bianma',editor: { xtype:'textfield'} },
					 { header: '联系人', dataIndex: 'lianxi',editor: { xtype:'textfield'} },
   					 ],
   			 height: 130,
			 width:'100%',
			 autoScroll:false,
			 
});
	

var payChannelStore = Ext.create('Ext.data.Store', {
    fields : [ 'classId', 'className'],
    proxy : {
        type : 'ajax',
        url : 'http://localhost:8080/queryType',
        reader : {
            type : 'json',
            root : 'data'
        }
    },
    autoLoad : true
});

var formpalcd=Ext.create('Ext.form.Panel', { //表单
    bodyPadding: 5,
    width:'100%',
    // 将会通过 AJAX 请求提交到此URL
   // url: 'save-form.php',
    // 表单域 Fields 将被竖直排列, 占满整个宽度
    layout: 'column',
    defaults: {
        anchor: '100%'
    },

    // The fields
    defaultType: 'textfield',
    items: [{
			xtype: 'textfield',
       		name: 'id',
       		id:"id",
			fieldLabel: '供应商编号',
       		labelWidth: 70
         
			},{
		style:'margin-left:9px',
		 xtype: 'textfield',
        name: 'fundsAttribution',
        fieldLabel: '账款归属',
        labelWidth: 70,
        anchor: '100%',
			},{
		 xtype: 'textfield',
        name:'fullName',
        fieldLabel: '供应商全称',
        labelWidth: 70,
        anchor: '100%',
		},{
				style:'margin-left:9px',
				 xtype: 'hiddenfield',
		        name: 'classId',
		        id:"classId",
		        fieldLabel: '类别',
				emptyText : '11',
		        labelWidth: 70,
		        anchor: '100%',
		       
			},{
			style:'margin-left:9px',
			 xtype: 'combo',
	        name: 'className',
	        id:"className",
	        fieldLabel: '类别',
	        labelWidth: 70,
	        anchor: '100%',
	        valueField : 'classId',
            queryMode : 'local',
            displayField : 'className',
			store : payChannelStore,
			listeners: {  
				select : function(cbox, record) {
	                var appId = record[0].data.classId;
	                Ext.getCmp('classId').setValue(appId);
	                orderStore.load({
	                    params : {
	                        start : 0,
	                        limit : itemPerPage,
	                        appIds : appId
	                    }
	                });
				}	
			}
			},{
		 xtype: 'textfield',
        name: 'shortName',
        fieldLabel: '供应商简称',
        labelWidth: 70,
		regex:/^[\u4E00-\u9FA5]+$/,//自定义正则表达式,(只能输入汉字);
		regexText:'叫你输入汉字啊,SB吗？',//提示
        anchor: '100%',
			},{
			style:'margin-left:9px',
		 xtype: 'textfield', //多行文本域
        name: 'areaName',
        fieldLabel: '地区',
        labelWidth: 70,
        anchor: '100%',
			},{
		 xtype: 'textfield',
        fieldLabel: '英文全称',
        name: 'engFullName',
		regex:/^[A-Za-z]+$/,//自定义正则表达式,(只能输入汉字);
		regexText:'叫你输入英文列啊,SB吗？',//提示
        labelWidth: 70,
        anchor: '100%',
			},{
			style:'margin-left:9px',
		 xtype: 'textfield',  //数值
        name: 'currencyName',
        fieldLabel: '币别',
        labelWidth: 70,
        anchor: '100%',
			},{
		 xtype: 'textfield', //日期
        fieldLabel: '英文简称',
        name: 'engShortName',
		regex:/^[A-Za-z]+$/,//自定义正则表达式,(只能输入汉字);
		regexText:'输入英文',//提示
        labelWidth: 70,
        anchor: '100%',
			},{
		style:'margin-left:9px',
		 xtype: 'textfield',
        name: 'address',
        fieldLabel: '公司地址',
        labelWidth: 70,
        anchor: '100%',
			},{
				xtype:"tabpanel",
				width:'100%',
				height:'220px',
				items:[{
					title:'基本资料',
					bodyPadding: 5,
						layout:"column",
					items:[{
				xtype: 'textfield',
				width:'120',
       			name: 'chiefName',
				fieldLabel: '负责人',
       			labelWidth: 70
			},{
			xtype: 'textfield',
       		name: 'personId',
			fieldLabel: '采购人',
			style:'margin-left:19px',
       		labelWidth: 70
			},{
			xtype: 'textfield',
       		name: 'linkMan',
			fieldLabel: '联系人',
       		labelWidth: 70
			},{
			xtype: 'textfield',
       		name: 'registration',
			fieldLabel: '税务登记号',
			style:'margin-left:19px',
       		labelWidth: 70
			},
			{
			xtype: 'textfield',
       		name: 'telephone1',
			fieldLabel: '联系电话',
       		labelWidth: 70
			},{
			xtype: 'textfield',
       		name: 'capitalization',
			fieldLabel: '资本额',
			style:'margin-left:19px',
       		labelWidth: 70
			},{
			xtype: 'textfield',
       		name: 'telephone2',
			fieldLabel: '联系电话二',
       		labelWidth: 70
			},{
			xtype: 'textfield',
			fieldLabel: '行业别',
       		name: 'industrialClass',
			style:'margin-left:19px',
       		labelWidth: 70
			},{
			xtype: 'textfield',
			fieldLabel: '电子邮件',
       		name: 'email',
			style:'margin-left:19px',
       		labelWidth: 70
			},{
	       		name: 'webAddress',
			xtype: 'textfield',
			fieldLabel: '网址',
       		labelWidth: 70
			},{
			xtype: 'textfield', 
       		name: 'faxNo',
			fieldLabel: '传真号码',
			style:'margin-left:19px',
       		labelWidth: 70
			}]
				},{
				title:"采购信息",
				bodyPadding: 5,
				style:'padding-top:5px',
				height:'200px',
				items:[
				{
			xtype: 'datefield',
       		name: 'latelyTradeDate',
			fieldLabel: '最近采购入库日',
       		labelWidth: 90
			},{
		 xtype: 'datefield',
        name: 'latelyOutDate',
        fieldLabel: '最近采购退货日',
        labelWidth: 90,
			},{
	             	fieldLabel: '单价是否含税',
					xtype:'combo',
	             	name: 'priceofTax',
	             	displayField: 'name',
					labelWidth:90,
    				valueField: 'abbr',
					store: Ext.create('Ext.data.Store', {
						fields: ['abbr', 'name'],
    					data : [
        					{"abbr":"含税", "name":"含税"},
        					{"abbr":"未税", "name":"未税"}
							
					   ]
					}),
					value: '含税',
        			typeAhead: true,
        			mode: 'local',
        			triggerAction: 'all',
        			selectOnFocus:true,
        			
	         	},{
		 xtype: 'datefield',
        name: 'invalidDate',
        fieldLabel: '终止交易日',
        labelWidth: 90,
			}
				]
				
			},{
				title:"付款信息",
				bodyPadding: 5,
				layout:"column",
				height:'200px',
				items:[{
					xtype:'label',
					text:'额度设定',
					style:'border-bottom:1px solid blue;color:#350257;',
				},{
					xtype:'label',
					text:'账款信息',
					margin:'0 190',
					style:'border-bottom:1px solid blue;color:#350257;',
				},
				{
				xtype: 'textfield',
				width:'120',
       			name: 'amountQuota',
				margin:"2",
				fieldLabel: '账款额度',
       			labelWidth: 60
			},{
				xtype: 'textfield',
				width:'120',
       			name: 'startAdvRecv',
				margin:"2",
				fieldLabel: '期初预付款',
       			labelWidth: 70
			},{
				xtype: 'textfield',
				width:'120',
       			name: 'remainQuota',
				margin:"2",
				fieldLabel: '剩余额度',
       			labelWidth: 60
			},{
				xtype: 'textfield',
				width:'120',
       			name: 'startReceivable',
				margin:"2",
				fieldLabel: '期初应付款',
       			labelWidth: 70
			},{
				xtype: 'textfield',
				width:'120',
       			name: 'curAdvRecv',
				margin:"2",
				fieldLabel: '期末预付款',
       			labelWidth: 70
			},{
	             	fieldLabel: '信用等级',
					xtype:'combo',
	             	name: 'creditLevel',
	             	displayField: 'name',
					labelWidth:63,
    				valueField: 'abbr',
					store: Ext.create('Ext.data.Store', {
						fields: ['abbr', 'name'],
    					data : [
        					{"abbr":"A", "name":"A"},
        					{"abbr":"B", "name":"B"},
							{"abbr":"C", "name":"C"},
							{"abbr":"D", "name":"D"},
							{"abbr":"E", "name":"E"},
							{"abbr":"F", "name":"F"}
					   ]
					}),
					value: 'A',
        			typeAhead: true,
        			mode: 'local',
        			triggerAction: 'all',
        			selectOnFocus:true,
        			
	         	},{
				xtype: 'textfield',
				width:'120',
       			name: 'curReceivable',
				margin:"2",
				fieldLabel: '期末应付款',
       			labelWidth: 70
			     }]
		      },{
				title:"地址",
				items:grids,
			},{
				title:"其他",
				height:200,
				bodyPadding: 15,
				items:[{
     		   	xtype     : 'textarea',
				name:'memo',
       		    fieldLabel: '备注',
				width:400, 
				labelWidth: 30,
				height:'100%'
				}]
			}]
		}]
});

var datas;
var datas1;
query(0);
function query(id){
	Ext.Ajax.request({
		url : "http://localhost:8080/querySupplierPage",
		params : {currentPage:id},
		method : "post",
		success : function(data) {
			if(data==""){
				Ext.MessageBox.alert("提示","目前没有供应商信息！！！",function(){});
			}else{
				formpalcd.getForm().reset();
				datas=JSON.parse(data.responseText);
				formpalcd.getForm().setValues(Ext.decode(data.responseText).pageobj[0]);
			}
		},
	});
}


var mainWindow = new Ext.Window({
		
		width:530,
		height:428,
		title:"供应商主文件",
		closable:true,
		resizable:false, //设置是否可以改变大小
		draggable:true, //设
		items:[formpalcd],
		dockedItems: [{
		    xtype: 'toolbar',
		    dock: 'top',
		    items: [
		        { xtype: 'button', text: '新增',handler: function() {
		        	
		        	formpalcd.getForm().reset();
		        	Ext.Ajax.request({
		        		url : "http://localhost:8080/queryID",
		        		params : {},
		        		method : "post",
		        		success : function(data) {
		        			var temp=JSON.parse(data.responseText);
		        			Ext.getCmp('id').setValue(temp);
		        			Ext.Ajax.request({
				        		url : "http://localhost:8080/queryType",
				        		params : {},
				        		method : "post",
				        		success : function(data) {
				        			var temp=JSON.parse(data.responseText);
				        			Ext.getCmp('className').setValue(temp.className);
				        		},
				        	});
		        		},
		        	});
		        	
		        	
		        }},
		        { xtype: 'button', text: '保存',handler: function() {
		        	Ext.Ajax.request({
						url:'http://localhost:8080/insertSupplier',
						headers:{"Content-Type":"application/json"},
						params:JSON.stringify(formpalcd.getForm().getValues()),
						success:function(response){
							Ext.MessageBox.alert("提示","处理成功",function(){});
						},
						failure: function(response, opts) {
		        	         console.log('供应商编号已经存在！！！' );
		        	     }
					});
		        }},
		        { xtype: 'button', text: '',handler: function() {}},
		        { xtype: 'button', text: '',handler: function() { }},
		        { xtype: 'button', text: '第一条',handler: function() { query(0);
	    			grids.getStore().removeAll();
	    		}},
		        { xtype: 'button', text: '上一条' ,handler : function() {
	    			if(datas.currentPage<datas.topPage){
	    				Ext.MessageBox.alert("提示","您正在浏览第一条数据!!!",function(){});
	    				return;
	    			}
	    			alert(datas.topPage);
	    			query(datas.topPage);
	    			grids.getStore().removeAll();
	    		}},
		        { xtype: 'button', text: '下一条' ,
		    		handler : function() {
		    			if(datas.nextPage>datas.pagesize-1){
		    				Ext.MessageBox.alert("提示","您正在浏览最后一条数据!!!",function(){});
		    				return;
		    			}
		    			query(datas.nextPage);
		    			grids.getStore().removeAll();
		    		}},
		        { xtype: 'button', text: '最后一条'  ,handler : function() {
		        	query(datas.pagesize-1);
	    			grids.getStore().removeAll();
	    		}}
		    ]
	    }],
		bbar:[{xtype:"splitbutton",text:'设定',width:90,menu:fielmenu},{xtype:"splitbutton",text:'功能',width:90,menu:fielmenu2}],
	});

function fromaction(){
	alert(Ext.encode(formpalcd.getForm().getValues()));// 序列化表单
}